using System;
using System.Collections.Generic;
using UnityEngine;


public class PlayerInput : MonoBehaviour
{
    public bool enableInput = true;

    public string XAxis = "Joy0X";
    public string YAxis = "Joy0Y";

    public string leftAxisCallback = "OnMove";

    public int joystickID = 1;          // 玩家1是1，玩家2是2
    
    // 0:方块   1: X  2:圆  3:三角
    private string[] RButtonCallBacks = {"PickCoin", "DropCoin", "FlyForward", "CastSkill"};  // 4个按键的回调
    // private List<string> RButtonCallBacks = new List<string>(4);

    Dictionary<int, List<UnityEngine.KeyCode>> buttonKeyCodeMapping = new Dictionary<int, List<UnityEngine.KeyCode>>();

    public float axisSensity = 0.3f;

    void Start() {
        // Debug.Log(RButtonCallBacks.Length.ToString());

        List<UnityEngine.KeyCode> list1 = new List<UnityEngine.KeyCode>();
        list1.Add(KeyCode.Joystick1Button0);
        list1.Add(KeyCode.Joystick1Button1);
        list1.Add(KeyCode.Joystick1Button2);
        list1.Add(KeyCode.Joystick1Button3);
        buttonKeyCodeMapping.Add(1, list1);

        List<UnityEngine.KeyCode> list2 = new List<UnityEngine.KeyCode>();
        list2.Add(KeyCode.Joystick2Button0);
        list2.Add(KeyCode.Joystick2Button1);
        list2.Add(KeyCode.Joystick2Button2);
        list2.Add(KeyCode.Joystick2Button3);
        buttonKeyCodeMapping.Add(2, list2);

        List<UnityEngine.KeyCode> list3 = new List<UnityEngine.KeyCode>();
        list3.Add(KeyCode.Joystick3Button0);
        list3.Add(KeyCode.Joystick3Button1);
        list3.Add(KeyCode.Joystick3Button2);
        list3.Add(KeyCode.Joystick3Button3);
        buttonKeyCodeMapping.Add(3, list3);

        List<UnityEngine.KeyCode> list4 = new List<UnityEngine.KeyCode>();
        list4.Add(KeyCode.Joystick4Button0);
        list4.Add(KeyCode.Joystick4Button1);
        list4.Add(KeyCode.Joystick4Button2);
        list4.Add(KeyCode.Joystick4Button3);
        buttonKeyCodeMapping.Add(4, list4);
    }

    void Update()
    {
        if (enableInput)
        {
            // 左摇杆的输入SendMessage到指定的callback
            float horizontal = -Input.GetAxis(XAxis);
            float vertical = Input.GetAxis(YAxis);

            horizontal = Mathf.Abs(horizontal) > axisSensity ? horizontal : 0;
            vertical = Mathf.Abs(vertical) > axisSensity ? vertical : 0;

            Vector2 leftAxisVec = new Vector2(horizontal, vertical);
            gameObject.SendMessage(leftAxisCallback, leftAxisVec, SendMessageOptions.DontRequireReceiver);

            // 获取手柄按键然后SendMessage到指定的callback
            for (int i = 0; i < 4; i++)
            {
                // Debug.Log(i.ToString());
                // Debug.Log(RButtonCallBacks.Length.ToString());
                string callbackName = RButtonCallBacks[i];
                UnityEngine.KeyCode keycode = buttonKeyCodeMapping[joystickID][i];

                if (!string.IsNullOrEmpty(callbackName) && Input.GetKeyDown(keycode))
                {
                    // Debug.Log("KeyDown()" + i + "," + joystickID);
                    gameObject.SendMessage(callbackName, SendMessageOptions.DontRequireReceiver);
                }
            }
        }
    }
}
